package com.rh.bmw4s.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.view.CommonHeader;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rh.bmw4s.R;
import com.rh.bmw4s.adapter.CategoryDetailAdapter;
import com.rh.bmw4s.bean.BaseBean;
import com.rh.bmw4s.bean.BrandBean;
import com.rh.bmw4s.comm.Constant;
import com.rh.bmw4s.inter.OpenItemIner;
import com.rh.bmw4s.utills.DealResult;
import com.rh.bmw4s.utills.DialogUtils;
import com.rh.bmw4s.utills.LogUtils;
import com.rh.bmw4s.utills.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新车选择车型(因为只有宝马所以单独写了)
 */
public class NewCarSelectTypeActivity extends BaseActivity implements OpenItemIner {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.ll_newcartype_baoma)
    LinearLayout linearLayout;
    @BindView(R.id.tv_back_newcar)
    TextView tvBack;
    @BindView(R.id.newcar_recyclerView_detail)
    LRecyclerView recyclerViewDetail;
    @BindView(R.id.newcar_drawerlayout)
    DrawerLayout drawerLayout;

    private CategoryDetailAdapter adapter;
    private LRecyclerViewAdapter lrecyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newcar_selecttype);
        ButterKnife.bind(this);

        initView();
        initDetailView();
    }

    private void initView() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
            }

            @Override
            public void onDrawerOpened(View drawerView) {
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
            }
        });
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("选择车型");
    }

    private void initDetailView() {
        recyclerViewDetail.setLayoutManager(new LinearLayoutManager(NewCarSelectTypeActivity.this));
        adapter = new CategoryDetailAdapter(NewCarSelectTypeActivity.this);
        //adapter.setDataList(new ArrayList<BrandBean>());
        lrecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerViewDetail.setAdapter(lrecyclerViewAdapter);
        CommonHeader header = new CommonHeader(NewCarSelectTypeActivity.this, R.layout.head_category_grid);
        //lRecyclerViewAdapter.addHeaderView(header);
        DividerDecoration divider = new DividerDecoration.Builder(NewCarSelectTypeActivity.this)
                .setHeight(R.dimen.divider_height_1)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_1)
                .build();
        recyclerViewDetail.addItemDecoration(divider);
        recyclerViewDetail.setLoadMoreEnabled(false);
        recyclerViewDetail.setPullRefreshEnabled(false);
        recyclerViewDetail.refresh();
        lrecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
            }
        });
    }

    private void getDatas() {
        DialogUtils.showProgressDlg(NewCarSelectTypeActivity.this, "正在加载...");
//        OkGo.<String>get("http://4s.liexianghudong.com/home/login/new_selection")
//                .tag(this)
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(Response<String> response) {
//                        LogUtils.d("fff", response.body().toString());
//                        DialogUtils.stopProgressDlg();
//                        BaseBean<BrandBean> bean = DealResult.getInstace().dealData(NewCarSelectTypeActivity.this, response, new TypeToken<BaseBean<BrandBean>>() {
//                        }.getType());
//                        List<BrandBean> beans = bean.getDatas();
//                        if (beans == null) {
//                            return;
//                        }
//                        setDetailDatas(beans);
//                    }
//
//                    @Override
//                    public void onError(Response<String> response) {
//                        DialogUtils.stopProgressDlg();
//                        ToastUtils.toastForShort(NewCarSelectTypeActivity.this, getString(R.string.bad_net));
//                        super.onError(response);
//                    }
//                });

        OkGo.<String>post(Constant.SELECTION_DETAIL)
                .tag(this)
                .params("id", 2)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        BaseBean<BrandBean> bean = DealResult.getInstace().dealData(NewCarSelectTypeActivity.this,
                                response, new TypeToken<BaseBean<BrandBean>>() {
                                }.getType());
                        List<BrandBean> beans = bean.getDatas();
                        if (beans == null) {
                            return;
                        }
                        setDetailDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(NewCarSelectTypeActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    public void setDetailDatas(List<BrandBean> beans) {
        BrandBean.BrandsBean brandsBean = new BrandBean.BrandsBean();
        List<BrandBean.BrandsBean> brandsBeanList = new ArrayList<>();
        brandsBean.setModel_name("不限车系");
        brandsBeanList.add(brandsBean);
        BrandBean bean = new BrandBean();
        bean.setBrands(brandsBeanList);
        bean.setLetters("不限车系");
        beans.add(0, bean);
        adapter.setDataList(beans);
        recyclerViewDetail.refresh();
    }

    @OnClick({R.id.title_left_one_btn, R.id.ll_newcartype_baoma, R.id.tv_back_newcar})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                finish();
                break;
            case R.id.ll_newcartype_baoma:
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                adapter.clear();
                drawerLayout.openDrawer(Gravity.RIGHT);
                getDatas();
                break;
            case R.id.tv_back_newcar:
                if (drawerLayout != null) {
                    if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                        drawerLayout.closeDrawers();
                    }
                }
                break;
        }
    }

    @Override
    public void itemClick(int id) {

    }

    @Override
    public void itemDetailClick(BrandBean.BrandsBean bean) {
        Intent i = new Intent();
        i.putExtra("bean", bean);
        Log.d("CCC", bean.getModel_name());
        this.setResult(1, i);
        this.finish();
    }
}
